var mainScroll;
$(document).ready(function(){
	// 初始化页面
	initCompont();
	// 获取验证码
	getValidateCodeFun();
	// 授权方法
	authorizeMethod();
	// 手动关闭提示方法
	closePromptByHand();
	// 显示合同页
	showContract();
});

// 初始化页面
function initCompont(){
	getSessionKey();
	mainIscroll();
}
//获取register号码和电话号码方法
function getSessionKey(){
	var phoneNumber = "";
	var fundLoginNumber = "";
	/*
	var localUrl = location.href;
	if(localUrl.indexOf("?") != -1){
		var paramInfo = localUrl.substr(localUrl.indexOf("?")+1);
		if(paramInfo.indexOf("&") != -1){
			var paramArray = paramInfo.split("&");
			for(var k = 0;k < paramArray.length;k ++){
				var pa = paramArray[k].split("=");
				if(pa[0] == "fundLoginNumber"){
					fundLoginNumber = pa[1];
				}
				if(pa[0] == "mobile"){
					var mobile = pa[1];
					if(mobile == null || mobile == ""|| mobile == "null"){
						phoneNumber = "未设置电话号码";
					}else{
						phoneNumber = pa[1];
					}
				}
			}
		}else{
			if(paramInfo.split("=")[0]=="sessionKey"){
				sessionKey = paramInfo.split("=")[1];
			}
		}
	}
	*/
	fundLoginNumber = getQueryParam("fundLoginNumber");
	phoneNumber = getQueryParam("mobile");
	sessionKey = getQueryParam("sessionKey");
	if(phoneNumber=="" || phoneNumber=="undefined")
		phoneNumber = "未设置电话号码";
	$("#fundLoginId").text(fundLoginNumber);
	$("#phoneId").text(phoneNumber);
}
//获取验证码
function getValidateCodeFun(){
	$("#validateCodeBtn").one("tap", function(){
		getValidateCode();
	});
}
// 获取验证码
function getValidateCode(){
	var phoneId = $("#phoneId").text();
	if(phoneId === "未设置电话号码" || phoneId==""){
		setPromptForInput("您没有设置电话号码,不能获取验证码,请到PS系统设置.");
		$("#validateCodeBtn").one("tap",function(){ getValidateCode(); });
	}else{
		$("#validateCodeBtn").css("background-color","#ccc");
		var timeCount = 60;
		setValidateTimeOut(timeCount);
		setPromptForInput("验证码已发送至"+phoneId+"，请注意查收");
		$.ajax({
			type: "post",
			url: mpsurl + "/mobile/otpYesKey/generateTmpSMSOTP.do",
			dataType: "text",
			data: {method:"getValidataCode"},
			success: function(data){
				var returnInfo = eval("("+data+")");
				var resultCode = returnInfo.resultCode;
				if(resultCode == "500600"){
					
				}else{
					var resultMessage = returnInfo.resultMessage;
					checkLogin(resultCode,"",resultMessage);
					//setPromptForInput(resultMessage);
				}
			},
			error: function(XMLHttpRequest,textStatus,errorThrown){
				setPromptForInput("网络异常，请检查网络后重试");
			}
		});
	}
}

// 授权方法
function authorizeMethod(){
	$("#J-BtnExit").on("tap", function(){
		/*
		//安卓关闭键盘
		var ua = navigator.userAgent;
		if(ua.indexOf("iPhone")>=0){
		}else{
			window.pa_app.closeKeyboard();
		}
		*/
		var fundPass = $("#fundPass").val();
		var validateCode = $("#validateCode").val();
		var isAgree = $("label.label-checkbox").hasClass("label-checkbox-on");
		var flag = true;
		if(fundPass == "" || fundPass == null){
			flag = false;
			setPromptForInput("请输入基金交易密码.");
			return;
		}
		if(validateCode == "" || validateCode == null){
			flag = false;
			setPromptForInput("请输入手机收到的验证码，验证码可点击“获取验证码”来获取.");
			return;
		}
		if(!isAgree){
			flag = false;
			setPromptForInput("请勾选同意协议.");
			return;
		}
		if(flag){
			loginPrompt();
			$.ajax({
				type: "post",
				url: mpsurl+"/mobile/fund/fundAccredit.do",
				dataType: "text",
				data: {fundLoginPassword:fundPass,otp:validateCode},
				success: function(data){
					var returnInfo = eval("("+data+")");
					var resultCode = returnInfo.resultCode;
					if(resultCode == "500600"){
						var sessionKey = returnInfo.sessionKey;
						var fundInitFlag = returnInfo.fundInitFlag;
						if(null !=  sessionKey){
							//location.href="/mobile/myfund/mszq.html?sessionKey="+sessionKey+"&fundInitFlag="+fundInitFlag;
							var param="sessionKey="+sessionKey+"&fundInitFlag="+fundInitFlag;
							gotoLocation("/mobile/myfund/mszq.html",param);
						}
					}else{
						$("#loginPromptBox").remove("slow");
						var resultMessage = returnInfo.resultMessage;
						setPromptForInput((resultMessage!="")?resultMessage:"网络异常，请检查网络后重试");
					}
				},
				error: function(XMLHttpRequest,textStatus,errorThrown){
					$("#loginPromptBox").remove("slow");
					setPromptForInput((errorThrown!="")?errorThrown:"网络异常，请检查网络后重试");
				}
			});
		}
	});
}
// 返回主页
function returnHome(){
	/*
	//安卓关闭键盘
	var ua = navigator.userAgent;
	if(ua.indexOf("iPhone")>=0){
	}else{
		window.pa_app.closeKeyboard();
	}
	*/
	setTimeout(function(){
		gotoLocation("/mobile/index.html","");
	},200);
}
//添加主页面滚动
function mainIscroll(){
	var wh = $(window).height()-90; 
	if(mainScroll instanceof iScroll)
		mainScroll.destroy();
	$("#content").css("height",wh+"px");
	mainScroll = new iScroll("content");
}

// 显示合同方法
function showContract(){
	$("#contractBtn").on("tap", function(){
		//location.href = "/mobile/myfund/contractInfo.html";
		gotoLocation("/mobile/myfund/contractInfo.html","");
	});
}
// 超时设置
function setValidateTimeOut(timeCount){
	var i = setInterval(function(){
		if(timeCount == 0){
			$("#validateCodeBtn").one("tap",function(){ getValidateCode(); });
			$("#validateCodeBtn").css("background-color","#007aff");
			$("#validateCodeBtn").html("获取验证码");
			clearInterval(i);
		}else{
			$("#validateCodeBtn").html(timeCount);
		}
		timeCount --;
	},1000);
}
/*
 * 登录提交跳转模态提示
 */
function loginPrompt(){
	var promptBox = "<div id='loginPromptBox' style='position:fixed;left:0;top:0;width:100%;height:100%;z-index:1000;display: block;'>"+
		"<div style='position:absolute;width:100%;height:100%;background-color:#000;opacity:.5;z-index:1000;'></div>"+
		"<div style='position: fixed;left: 30%;top: 40%;width: 40%;z-index: 1001;'>"+
		"<div style='position: absolute;width: 100%;height: 100%;background: #000;-webkit-border-radius: 8px;border-radius: 8px;opacity: .0;z-index: 1;'>"+
		"</div>"+
		"<div style='position: relative;display: -webkit-box;color: #fff;min-height:44px;-webkit-box-orient: horizontal;-webkit-box-pack: center;padding:28px 18px;font-size: 30px;text-align: center;z-index: 2;'>"+
		"<img src='../images/loading.gif' width='70px' height='70px'/></div>"+
		"</div>"+
		"</div>";
	$("body").append(promptBox);
}